import {basePath} from "@/config/env.js"
import request from '@/router/axios'

export function ajaxDownload(url,params) {
	request(url,  {
		method: 'post',
		data:params,
	    responseType: 'blob'
	}).then(res =>{
	    let blob = new Blob([res.data]);
	    let objectUrl = URL.createObjectURL(blob);

        let fileName = res.headers['content-disposition'];
        if (!fileName){
        	fileName = res.headers['Content-Disposition'];
        }
        if (fileName && fileName.indexOf('attachment;fileName=')==0){
        	fileName = fileName.substr('attachment;fileName='.length);
        }
        if (!fileName){
        	fileName = "未命名";
        }

        if (window.navigator && window.navigator.msSaveOrOpenBlob) { // IE
          window.navigator.msSaveOrOpenBlob(blob, fileName);
        } else {
          let objectUrl = (window.URL || window.webkitURL).createObjectURL(blob);
          let downFile = document.createElement('a');
          downFile.style.display = 'none';
          downFile.href = objectUrl;
          downFile.download = fileName; // 下载后文件名
          document.body.appendChild(downFile);
          downFile.dispatchEvent(new MouseEvent('click', {bubbles: true, cancelable: true, view: window}));
          //downFile.click();
          document.body.removeChild(downFile); // 下载完成移除元素
          // window.location.href = objectUrl
          window.URL.revokeObjectURL(objectUrl);   // 只要映射存在，Blob就不能进行垃圾回收，因此一旦不再需要引用，就必须小心撤销URL，释放掉blob对象。
        }	    
	})
}


export default function download(url,params) {
  if (url && url.length > 0) {
    var strParam = '';
    if(params){
      for (var prop in params) {
         //prop = $.trim(prop);
         strParam = strParam+prop+"="+encodeURI(encodeURI(params[prop]))+"&";
      }
      //strParam = encodeURI(encodeURI(strParam));
      if(url.indexOf('?') > -1) {
          url = url+"&"+strParam;
      }else{
          url = url+'?'+strParam;
      }
      if (url.toLowerCase().indexOf('http:')!=0&&url.toLowerCase().indexOf('https')!=0){
    	    if (basePath.lastIndexOf('/')!=basePath.length-1 && url.indexOf('/')!=0)
    	      url = basePath + '/' + url;
    	    else
    	      url = basePath + url;
      }
    }
    var iframe = top.document.getElementById("download_iframe");
    if (!iframe) {
      //create print iframe
      var el = top.document.createElement('IFRAME');
      el.id = "download_iframe";
      el.src = "about:blank";
      el.height = 0;
      el.width = 0;
      el.style = "height: 0px; width: 0px; display: none;";
      top.document.body.appendChild(el);
			
      iframe = el;
    }
    //var mask = layer.load();
    iframe.setAttribute('src',url);
  } else {
    alert("无法下载：URL为空~~!");
  }
}

export function postDownload(url,params) {
  if (url && url.length > 0) {
      if (url.toLowerCase().indexOf('http:')!=0&&url.toLowerCase().indexOf('https')!=0){
  	    if (basePath.lastIndexOf('/')!=basePath.length-1 && url.indexOf('/')!=0)
  	      url = basePath + '/' + url;
  	    else
  	      url = basePath + url;
    }
    var strParam = JSON.stringify(params);
    var iframe = top.document.getElementById("post_download_iframe");
    if (!iframe) {
      //create print iframe
      var el = top.document.createElement('IFRAME');
      el.id = "post_download_iframe";
      el.src = "about:blank";
      el.height = 0;
      el.width = 0;
      el.style = "height: 0px; width: 0px; display: none;";
      top.document.body.appendChild(el);
      iframe = el;
    }
		iframe.contentDocument.body.innerHTML="<form id='form' method='post'><input type='hidden' id='param' name='param'></input></form>"
		iframe.contentDocument.getElementById("param").setAttribute("value",strParam);
		iframe.contentDocument.getElementById("form").setAttribute("action",url);
		iframe.contentDocument.getElementById("form").submit();
  } else {
    alert("无法下载：URL为空~~!");
  }
}

